package org.example.emos.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.example.emos.common.util.R;
import org.example.emos.config.shiro.ThreadLocalToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TokenAspect {
    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * org.example.emos.controller..*.*(..))")
    public void aspect(){}

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        R r = (R) point.proceed();
        String token = threadLocalToken.getToken();
        //如果ThreadLocal 中存在Token，说明是更新的Token
        if(token != null){
            r.put("token",token);//让相应中放置Token
            threadLocalToken.clear();
        }
        return r;
    }
}
